<?php
/**
 * @title 743. 网络延迟时间
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $times
     * @param Integer $n
     * @param Integer $k
     * @return Integer
     */
    function networkDelayTime($times, $n, $k) {
        /**
         * @since 2021-08-02 传输到该点
         */
        $pointTimeArray = [
            $k => 0,
        ];

        /**
         * @since 2021-08-02 迭代
         */
        $pointNextArray = array_fill(1, $n, []);
        foreach ($times as list($s, $e, $d)){
            $pointNextArray[$s][] = [$e, $d];
        }

        /**
         * @since 2021-08-02 起点, 层次遍历
         */
        $points = [$k];
        while($points){
            $nexts = [];

            /**
             * @since 2021-08-02 层所有点
             */
            foreach ($points as $point){
                /**
                 * @since 2021-08-02 起步距离
                 */
                $base = $pointTimeArray[$point];
                // echo "point={$point}, base distance={$base}", PHP_EOL;

                /**
                 * @since 2021-08-02 可以达到所有点
                 */
                foreach ($pointNextArray[$point] as list($e, $d)){
                    $d = $base+$d;

                    /**
                     * @since 2021-08-02 目标点是否存在
                     */
                    if(!isset($pointTimeArray[$e])){
                        $pointTimeArray[$e] = $d;
                        $nexts[] = $e;
                    } else {
                        if($d < $pointTimeArray[$e]){
                            $pointTimeArray[$e] = $d;
                            $nexts[] = $e;
                        } else {
                            /**
                             * @since 2021-08-02 更长的距离, 抛弃
                             */
                        }
                    }
                }
            }

            /**
             * @since 2021-08-02 迭代
             */
            $points = $nexts;
        }

        /**
         * @since 2021-08-02 是否到达所有点
         */
        if($n == count($pointTimeArray)){
            $result = max($pointTimeArray);
        } else {
            /**
             * @since 2021-08-02 有点无法到达
             */
            $result = -1;
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202108/2021.08.02_2.php
 */
$datas = [
    [[[2,1,1],[2,3,1],[3,4,1]], 4, 2], // 2
    [[[1,2,1]], 2, 1], // 1
    [[[1,2,1]], 2, 2], // -1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->networkDelayTime(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
